package com.flashcards;

import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.flashcards.vocabulary.Vocabulary;

public class Navigation3D {
    private FrameLayout container;
    private Vocabulary vocabulary;
    private TextView translationView;
    private TextView wordView;

    public Navigation3D(FrameLayout container, Vocabulary vocabulary) {
        this.container = container;
        this.vocabulary = vocabulary;
        translationView = (TextView) container.findViewById(R.id.translation);
        wordView = (TextView) container.findViewById(R.id.word);
        Word firstWord = vocabulary.first();
        wordView.setText(firstWord.getEng());
        translationView.setText(firstWord.getTranslation());
        Log.i(">>>>>", "first word in vocabulary " + firstWord.getEng() + "  " + firstWord.getTranslation());
    }

    public boolean onKey(int keyCode) {
        Word word = null;
//        if (!wordView.isShown()) {
//            container.showPrevious();
//        }
        container.requestFocus();
        container.setFocusableInTouchMode(true);
        CharSequence text = wordView.getText();
        Log.i(">>>>>>", "query with " + text);
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            word = vocabulary.previous(text.toString());
            Log.i(">>>>>>", "the next one is " + word.getEng());
            wordView.setText(word.getEng());
            translationView.setText(word.getTranslation());
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            word = vocabulary.next(text.toString());
            Log.i(">>>>>>", "the next one is " + word.getEng());
            wordView.setText(word.getEng());
            translationView.setText(word.getTranslation());
            return true;
        }
        return false;
    }
}
